package com.yaj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yaj.constant.LeaveConstants;
import com.yaj.pojo.ApprovalRecords;
import com.yaj.pojo.LeaveRequest;
import com.yaj.mapper.LeaverequestMapper;
import com.yaj.service.IApprovalRecordsService;
import com.yaj.service.ILeaveRequestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yaj.utils.UserThreadLocal;
import com.yaj.vo.LeaveRequestVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaojiu
 * @since 2024-08-19
 */

@Slf4j
@Service
public class LeaveRequestServiceImpl extends ServiceImpl<LeaverequestMapper, LeaveRequest> implements ILeaveRequestService {
    @Autowired
    private IApprovalRecordsService approvalRecordsService;

    @Override
    public List<LeaveRequestVo> getLeaveRequests() {
        return baseMapper.getLeaveRequests();
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void approveLeave(Long approverId, Long id) {
        LeaveRequest leaveRequest = baseMapper.selectById(id);
        //获取当前的userId
        leaveRequest.setStatus(LeaveConstants.LEAVE_REQUEST_APPROVED);
        leaveRequest.setApproverId(approverId);
        baseMapper.updateById(leaveRequest);
        //插入请假记录表
        ApprovalRecords approvalRecords = ApprovalRecords.builder()
                .leaveRequestId(leaveRequest.getId())
                .approvalStatus(leaveRequest.getStatus())
                .approverId(leaveRequest.getApproverId())
                .approvalTime(LocalDateTime.now())
                .build();
        approvalRecordsService.insertRecords(approvalRecords);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void rejectLeave(Long approverId, Long id) {
        LeaveRequest leaveRequest = baseMapper.selectById(id);
        //获取当前的userId
        leaveRequest.setApproverId(approverId);
        log.info("apid",approverId);
        leaveRequest.setStatus(LeaveConstants.LEAVE_REQUEST_REJECTED);
        baseMapper.updateById(leaveRequest);
        //插入请假记录表
        ApprovalRecords approvalRecords = ApprovalRecords.builder()
                .leaveRequestId(leaveRequest.getId())
                .approvalStatus(leaveRequest.getStatus())
                .approverId(leaveRequest.getApproverId())
                .approvalTime(LocalDateTime.now())
                .build();
        approvalRecordsService.insertRecords(approvalRecords);
    }

@Override
public List<LeaveRequestVo> getLeaveRecords(Long id) {
    LambdaQueryWrapper<LeaveRequest> wrapper = Wrappers.<LeaveRequest>lambdaQuery().eq(LeaveRequest::getUserId, id);
    List<LeaveRequest> leaveRequests = baseMapper.selectList(wrapper);
    // 使用 BeanUtil.copyToList 进行转换
    List<LeaveRequestVo> leaveRequestVoList = BeanUtil.copyToList(leaveRequests, LeaveRequestVo.class);
    return leaveRequestVoList;
}


}
